package com.mopa.http;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;

import com.mopa.bean.PicItem;

import android.util.Log;
public class UploadPicListener extends BaseRequestListener
{
	private Callback mCallback;
	public UploadPicListener(Callback callback)
	{
		mCallback = callback;
	}

	public void onComplete(String response, Object state)
	{
		super.onComplete(response, state);
		try
		{
			JSONObject obj = ParseResoneJson.parseJson(response);
			int ret = -10000;
			String msg = "";
			try
			{
				ret = obj.getInt("error_code");
				msg = obj.getString("error_msg");
			}
			catch (JSONException jsonexception) { }
			if (ret == -10000)
			{
				PicItem pic = new PicItem(obj.getInt("pid"), obj.getInt("aid"), obj.getInt("uid"), obj.getString("src"), obj.getString("src_small"),
						obj.getString("src_big"), obj.getString("caption"));
				mCallback.onSuccess(pic);
			} else
			{
				mCallback.onFail(ret, msg);
			}
		}
		catch (NumberFormatException e)
		{
			mCallback.onFail(0x80000000, e.getMessage());
			e.printStackTrace();
		}
		catch (JSONException e)
		{
			mCallback.onFail(0x80000000, e.getMessage());
			e.printStackTrace();
		}
		catch (CommonException e)
		{
			mCallback.onFail(0x80000000, e.getMessage());
			e.printStackTrace();
		}
		Log.e("debug", "UploadPicListener response:"+response);
	}

	public void onFileNotFoundException(FileNotFoundException e, Object state)
	{
		mCallback.onFail(0x80000000, (new StringBuilder("Resource not found:")).append(e.getMessage()).toString());
	}

	public void onIOException(IOException e, Object state)
	{
		mCallback.onFail(0x80000000, (new StringBuilder("Network Error:")).append(e.getMessage()).toString());
	}
}
